package com.kizuki.thread;

import org.junit.Test;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

public class TestSynchronousQueue {

    @Test
    public void test() throws InterruptedException {
        BlockingQueue<Integer> synchronousQueue = new SynchronousQueue<>();

        new Thread(()->{
            try {
                synchronousQueue.put(1);
                System.out.println("put 1");
                TimeUnit.SECONDS.sleep(1);
                synchronousQueue.put(2);
                System.out.println("put 2");
                TimeUnit.SECONDS.sleep(1);
                synchronousQueue.put(3);
                System.out.println("put 3");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(1);
                System.out.println(Thread.currentThread().getName() + "->" + synchronousQueue.take());
                System.out.println(Thread.currentThread().getName() + "->" + synchronousQueue.take());
                System.out.println(Thread.currentThread().getName() + "->" + synchronousQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        TimeUnit.SECONDS.sleep(8);

    }

}
